@extends('layouts.layouts')
@section('title')
    <title>后台首页</title>
    <style>
        .file {
            position: relative;
            display: inline-block;
            background: #D0EEFF;
            border: 1px solid #99D3F5;
            border-radius: 4px;
            padding: 4px 12px;
            overflow: hidden;
            color: #1E88C7;
            text-decoration: none;
            text-indent: 0;
            line-height: 20px;
        }
        .file input {
            position: absolute;
            font-size: 100px;
            right: 0;
            top: 0;
            opacity: 0;
        }
        .file:hover {
            background: #AADFFD;
            border-color: #78C3F3;
            color: #004974;
            text-decoration: none;
        }
        .form-horizontal .form-group {
            margin-right: 5px;
            margin-left: -1px;
        }
    </style>
@endsection
@section('content')
    <style>

        .form-horizontal .form-group {
            margin-right: 5px;
            margin-left: -1px;
        }
    </style>
    <link rel="stylesheet" href="/lib/layer/theme/default/layer.css"/>
    <script type="text/javascript" src="/lib/layer/layer.js"></script>
    <link href="/css/ueditor.css" type="text/css" rel="stylesheet">
    <div class="content">
        <div class="header">
            <ul class="breadcrumb">
                <li><a href="/thematic/news/push">消息推送</a>／<a href="#">推送管理</a> </li>

            </ul>
        </div>
        <div class="main-content">
            <ul class="nav nav-tabs nav-tabs-course">
                <li role="presentation" class="course_add active"><a href="javascript:void(0);">课程推送</a></li>
                <li><a href="javascript:void(0);">推送记录</a> </li>
            </ul>
            <form class="form-horizontal" id="courseFormInfo" action="/course/postCreate" method="post" style="">
                {{csrf_field()}}
                <div class="course_class">
                    <div class="col-md-10">
                        <br>
                        <div class="tab-content">

                            <div class="form-group">
                                <label for="inputPassword3" class="col-sm-3 control-label">推送内容</label>
                                <div class="col-sm-9">
                                    <select class="form-control themeType" name="theme" autocomplete="off" onchange="selectPushType(this)">
                                        <option>选择</option>
                                        <option value="1">专题</option>
                                        <option value="2">课程</option>
                                    </select>
                                </div>
                            </div>
                            <div class="form-group theme_news" style="display:none;">
                                <label for="inputPassword3" class="col-sm-3 control-label">专题内容选择</label>
                                <div class="col-sm-9">
                                    <button type="button" class="btn btn-primary btn-lg" data-page="1" onclick="themeSelect(this);">选择专题</button>&nbsp;&nbsp;<span style="color:blue;" id="themeId"></span>&nbsp;&nbsp;&nbsp;<button type="button" class="btn btn-primary btn-lg" data-page="1" onclick="articleSelect(this);">选择文章</button>&nbsp;&nbsp;<span style="color:blue;" id="articleId"></span>
                                    <input type="hidden" class="form-control" value="" id="inputPassword3" name="articleId">
                                    <input type="hidden" class="form-control" value="" id="inputPassword3" name="themeId">
                                </div>
                            </div>
                            <div class="form-group course_buy" style="display:none;">
                                <label for="inputPassword3" class="col-sm-3 control-label">推送课程</label>
                                <div class="col-sm-9">
                                    <button type="button" class="btn btn-primary btn-lg" data-page="1" onclick="selectCourse(this);">选择课程</button>&nbsp;&nbsp;<span style="color:blue;" id="courseId"></span>
                                    <input type="hidden" class="form-control" value="" id="inputPassword3" name="courseId">
                                </div>
                            </div>
                            <div class="form-group">
                                <label for="inputPassword3" class="col-sm-3 control-label">推送范围</label>
                                <div class="col-sm-9">

                                    <select class="form-control pushObject" name="pushObject" autocomplete="off">
                                        <option value="">选择</option>
                                        <option value="TEACHER">招生老师</option>
                                        <option value="">其他</option>
                                    </select>
                                </div>
                            </div>
                        </div>
                    </div>
                    <div class="row">
                        <p>
                            <button type="button" class="btn btn-primary btn-lg col-md-offset-5" data-id="" onclick="pushMessages(this);" style="width:120px;background:#286090;color:#fff;">一键推送</button>
                        </p>
                    </div>
                </div>
            </form>
        </div>
    </div>
    <div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
        <div class="modal-dialog" role="document">
            <div class="modal-content">
                <div class="modal-header">
                    <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
                    <h4 class="modal-title" id="myModalLabel">选择课程</h4>
                </div>
                <div class="modal-body"></div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
                    <button type="button" class="btn btn-primary">Save changes</button>
                </div>
            </div>
        </div>
    </div>
    <script src="/js/course.js"></script>
    <script src="/lib/jquery.validate.js" type="text/javascript"></script>
    <script>
        function selectPushType(obj){
            var type = $(obj).val();
            if(type == 1){
                $('.theme_news').css('display','block');
            }else{
                $('.theme_news').css('display','none');
            }
            if(type == 2){
                $('.course_buy').css('display','block');
            }else{
                $('.course_buy').css('display','none');
            }
        }
        //选择专题
        function themeSelect(obj){
            var page = $(obj).attr('data-page');
            var title = $('#themeTitle').val();

            var data = {page:page,title:title};
            $.ajax({
                url:'/theme/choose',
                data:data,
                type:'GET',
                dataType:'json',
                success:function(res){
                    if(res.code == 0){
                        $('#myModal .modal-body').html(res.body);
                        $('#myModal').modal('show');
                    }
                }
            });
        }
        //选择专题文章
        function articleSelect(obj){
            var page = $(obj).attr('data-page');
            var title = $('#articleTitle').val();
            var special_id = $("input[name='themeId']").val();
            var data = {page:page,title:title,special_id:special_id};
            $.ajax({
                url:'/article/choose',
                data:data,
                type:'GET',
                dataType:'json',
                success:function(res){
                    if(res.code == 0){
                        $('#myModal .modal-body').html(res.body);
                        $('#myModal').modal('show');
                    }
                }
            });
        }
        function articleChecked(obj){
            var class_id = $(obj).attr('data-id');
            var name = $(obj).attr('data-name');
            $("input[name='articleId']").val(class_id);
            $('#articleId').text(name);
            $('#myModal').modal('hide');
            $('#articleTitle').val('');
        }
        function themeChecked(obj){
            var class_id = $(obj).attr('data-id');
            var name = $(obj).attr('data-name');
            $("input[name='themeId']").val(class_id);
            $('#themeId').text(name);
            $('#myModal').modal('hide');
            $('#themeTitle').val('');
        }
        function pushMessages(obj){

            var _token = '{{csrf_token()}}';
            var type = $('.themeType').val();
            var pushObj = $('.pushObject').val();
            if(type == 1){
                var themeId = $("input[name='themeId']").val();
                var articleId = $("input[name='articleId']").val();
                var data = {_token:_token,type:type,aritcle_id:articleId,pushObj:pushObj};
            }else{
                var courseId = $("input[name='courseId']").val();
                var data = {_token:_token,type:type,courseId:courseId,pushObj:pushObj};
            }
            if(pushObj == ''){
                layer.alert('未选中推送人群');
                return false;
            }
//            var name = $('.courseType option:selected').text();
            var message = '确定推送?';
            layer.confirm(message, {
                btn: ['是', '否'],
            }, function(index){
                $.ajax({
                    url:'/define/push/type',
                    data:data,
                    type:'POST',
                    dataType:'json',
                    success:function(res){
                        layer.alert(res.message);
                        if(res.code == 0){
                            window.location.reload();
                        }
                    }
                });
                layer.close(index);
                return true;
            }, function(index){
                layer.close(index);
            });
        }

        $("#courseFormInfo").validate({
            onsubmit: true,// 是否在提交时验证
            submitHandler: function (form) {

                form.submit();
            }
        });
    </script>
    <style type="text/css">
        .col-sm-2{
            width:60px;
        }
        .course_content{
            margin-right: -30px;
            margin-left: -30px;
            margin-top:5px;
        }
        .btn-success{
            background-image:none!important;
        }
    </style>
@endsection